<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - geometry - cube</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>
	<body>

		<script type="module">

			import * as THREE from '../build/three.module.js';
			import { TrackballControls } from './jsm/controls/TrackballControls.js';

			let camera, scene, renderer;
			let controls;

			init();
			animate();

			function init() {

				//Create a WebGLRenderer and turn on shadows in the renderer
				renderer = new THREE.WebGLRenderer();
				renderer.shadowMap.enabled = true;
				renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default 	
				

				scene = new THREE.Scene();
				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 400;

				//Create a DirectionalLight and turn on shadows for the light
				let light = new THREE.PointLight( 0xffffff, 1, 1000 );
				light.position.set( 50, 50, 50 );
				light.castShadow = true; // default false
				scene.add( light );
				//Set up shadow properties for the light
				light.shadow.mapSize.width = 512; // default
				light.shadow.mapSize.height = 512; // default
				light.shadow.camera.near = 0.5; // default
				light.shadow.camera.far = 500; // default

				//Create a sphere that cast shadows (but does not receive them)
				let sphereGeometry = new THREE.SphereBufferGeometry( 5, 32, 32 );
				let sphereMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
				let sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
				sphere.castShadow = true; //default is false
				sphere.receiveShadow = false; //default
				scene.add( sphere );

				//Create a plane that receives shadows (but does not cast them)
				let planeGeometry = new THREE.PlaneBufferGeometry( 100, 100, 32, 32 );
				let planeMaterial = new THREE.MeshStandardMaterial( { color: 0x00ff00 } )
				let plane = new THREE.Mesh( planeGeometry, planeMaterial );
				plane.receiveShadow = true;
				plane.position.z=-10;
				scene.add( plane );

				//Create a helper for the shadow camera (optional)
				let helper = new THREE.CameraHelper( light.shadow.camera );
				scene.add( helper );

				scene.add( new THREE.AmbientLight( 0x111111 ) );
				
				//
				//renderer = new THREE.WebGLRenderer( { antialias: true } );
				
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );

				document.body.appendChild( renderer.domElement );
				controls = new TrackballControls( camera, renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );				

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );

				//mesh.rotation.x += 0.005;
			//	mesh.rotation.y += 0.01;
				controls.update();

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
